package com.teamall.web.app.controller;

import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.DateUtils;
import com.teamall.framework.util.UploadFileUtil;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.sys.db.po.SysFilePO;
import com.teamall.web.sys.db.po.SysPhotoPO;
import com.teamall.web.sys.service.ISysFileService;
import com.teamall.web.sys.service.ISysPhotoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.teamall.framework.util.UploadFileUtil.saveFiles;

@Controller
@RequestMapping(value = "/app/file/")
public class AppFilesController {
    @Autowired
    private ISysPhotoService sysPhotoService;
    @Autowired
    private ISysFileService sysFileService;
    /**
     * 保存文件
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadfiles", consumes = "multipart/form-data")
    @ResponseBody
    public ResResult uploadfiles(HttpServletRequest request) throws BaseAppException{
        ResResult resResult = new ResResult();
        List<Map> resultList = UploadFileUtil.saveFiles(request);
        resResult.setMsg("文件上传成功");
        resResult.setRspData(resultList);
        for (Map<String,String> map:resultList){
            SysPhotoPO po = new SysPhotoPO();
            po.setPhotoCode(map.get("imgId"));
            po.setFilePath(map.get("realPath"));
            sysPhotoService.add(po);
        }
        return resResult;
    }

    @RequestMapping(value = "/getFile")
    public void doPost(HttpServletRequest request, HttpServletResponse response,String imgId)
            throws ServletException, IOException ,BaseAppException{
        //读取本地图片输入流
        SysPhotoPO po = new SysPhotoPO();
        po.setPhotoCode(imgId);
        SysPhotoPO resultPO = sysPhotoService.selectByArg(po).get(0);
        FileInputStream inputStream = new FileInputStream(resultPO.getFilePath());
        int i = inputStream.available();
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        //记得关闭输入流
        inputStream.close();
        //设置发送到客户端的响应内容类型
        response.setHeader( "Content-disposition", "attachment;filename="+resultPO.getFileName());
        response.setHeader("Content-Type","image/jped");
        response.setHeader("Expires","0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Content-Transfer-Encoding","binary");
        response.setHeader("Pragma","public");
        response.setHeader("Content-Length",String.valueOf(i));
        OutputStream out = response.getOutputStream();
        out.write(buff);
        //关闭响应输出流
        out.close();
    }
    @RequestMapping(value = "/getReportFile")
    public void getReportFile(HttpServletRequest request, HttpServletResponse response,String imgId)
            throws ServletException, IOException ,BaseAppException{
        //读取本地图片输入流
        SysFilePO po = new SysFilePO();
        po.setPhotoCode(imgId);
        SysFilePO resultPO = sysFileService.selectByArg(po).get(0);
        FileInputStream inputStream = new FileInputStream(resultPO.getFilePath());
        int i = inputStream.available();
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        //记得关闭输入流
        inputStream.close();
        response.setHeader("Content-Type","image/jpeg");
        //设置发送到客户端的响应内容类型
        response.setHeader( "Content-disposition", "attachment;filename="+ URLEncoder.encode(resultPO.getFileName(), "utf-8"));
        response.setHeader("Expires","0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Content-Transfer-Encoding","binary");
        response.setHeader("Pragma","public");
        response.setHeader("Content-Length",String.valueOf(i));
        OutputStream out = response.getOutputStream();
        out.write(buff);
        //关闭响应输出流
        out.close();
    }
}